package com.yehuishou.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.Address;
import com.yehuishou.pojo.Identity;
import com.yehuishou.pojo.LeaseItem;
import com.yehuishou.pojo.Order;
import com.yehuishou.pojo.User;
import com.yehuishou.pojo.Xianshiqi;
import com.yehuishou.pojo.Zldd;
import com.yehuishou.pojo.Zulinorder;
import com.yehuishou.service.LeaseItemService;
import com.yehuishou.service.UserService;

@Controller
public class LeaseItemController {

	@Autowired
	private LeaseItemService liser;
	@Autowired
	private UserService userService;

	// 查询所有套餐
	@RequestMapping("leaseitem")
	public String findAll(Model model) {
		List<LeaseItem> item = liser.findAll();
		model.addAttribute("item", item);
		return "zupin01";
	}

	// 通过id查询套餐
	@RequestMapping("zulinxq")
	public String findByRid(Integer rid, Model model, HttpSession session) {
		session.setAttribute("token", new Date().getTime() + "");
		LeaseItem item = liser.findByRid(rid);
		List<Xianshiqi> list = liser.findXSQAll();
		model.addAttribute("item", item);
		model.addAttribute("list", list);
		model.addAttribute("id", list.get(0).getId());

		return "zupin02";
	}

	// 提交自定义套餐
	@ResponseBody
	@RequestMapping("tij")
	public Map<String, Object> tij(Zulinorder zlo, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean fale;
		User user = (User) session.getAttribute("user");
		if (user == null) {
			fale = false;
			msg = "未登录";
		} else {
			int sjs = (int) ((Math.random() * 9 + 1) * 10000);
			zlo.setOid(new Date().getTime() + "" + sjs);
			zlo.setCombo_name("自定义套餐");
			liser.savezlo(zlo);
			Order or = new Order();
			or.setOid(zlo.getOid());
			or.setCreatetime(new Date());
			or.setUid(user.getUid());
			or.setOrder_type(3);
			or.setStatus(1);
			liser.saveor(or);
			Address adres = userService.findAByUid(user.getUid());
			if(adres == null) {
				Address addr = new Address();
				addr.setUid(user.getUid());
				userService.saddress(addr);
			}
			fale = true;
			msg = "提交成功";
		}
		map.put("fale", fale);
		map.put("msg", msg);
		return map;
	}

	// 判断是什么用户
	@ResponseBody
	@RequestMapping("pdu")
	public Map<String, Object> pdu(Integer genders, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean fale;
		User user = (User) session.getAttribute("user");
		if (user == null) {
			fale = false;
			msg = "未登录";
		} else {
			if (user.getState() == genders) {
				fale = true;
			} else {
				Identity iden = userService.findIById(user.getUid());
				Integer a = iden.getIstate();
				if (a == 0) {
					fale = false;
					msg = "未认证";
				} else if (a == 1) {
					fale = false;
					msg = "未审核";
				} else if (a == 3) {
					fale = false;
					msg = "审核失败";
				} else {
					fale = false;
					msg = "不是此类型客户";
				}
			}
		}
		map.put("msg", msg);
		map.put("fale", fale);
		return map;
	};

	// 生成订单
	@RequestMapping("xsqdd")
	public String xsqdd(Integer rid, Integer xsqid, Integer tim, Integer num, Integer tai, Integer genders,
			HttpSession session, Model model) {
		LeaseItem item = liser.findByRid(rid);
		Xianshiqi xsqs = liser.findXSQId(xsqid);
		User user = (User) session.getAttribute("user");
		String obj = (String) session.getAttribute("token");
		Zulinorder zlo = new Zulinorder();
		Order or = new Order();
		if (obj == null) {
			String oid = (String) session.getAttribute("oid");
			zlo = liser.findByZL(oid);
			or = liser.findByO(oid);
		} else {
			session.removeAttribute("token");
			int sjs = (int) ((Math.random() * 9 + 1) * 10000);
			zlo.setOid(new Date().getTime() + "" + sjs);
			if (num == 0) {
				zlo.setTime(tim + "天");
			} else {
				zlo.setTime(tim + "个月");
			}
			zlo.setNum(tai);
			zlo.setImg(item.getImg());
			zlo.setCombo_name(item.getCombo_name());
			zlo.setBrand(item.getBrand());
			zlo.setCpu(item.getCpu());
			zlo.setNeicuen(item.getNeicuen());
			zlo.setXianka(item.getXianka());
			zlo.setYingpan(item.getYingpan());
			zlo.setXiansp(xsqs.getName());
			zlo.setXsqj(xsqs.getPrice());
			zlo.setZheko(item.getZheko());
			zlo.setRid(item.getRid());
			if (num == 0) {
				if (tim <= 30) {
					zlo.setDanjia(item.getOneday());
				} else if (tim > 30 && tim <= 90) {
					zlo.setDanjia(item.getOnemonth());
				} else {
					zlo.setDanjia(item.getThreemonth());
				}
			} else {
				if (tim == 1) {
					zlo.setDanjia(item.getOneday());
				} else if (tim > 1 && tim <= 3) {
					zlo.setDanjia(item.getOnemonth());
				} else {
					zlo.setDanjia(item.getThreemonth());
				}
			}

			if (genders == 4) {
				if (tai >= 10) {
					zlo.setYaj(item.getYajin() * 0.5);
				} else {
					zlo.setYaj(item.getYajin());
				}
			} else {
				if (tai >= 3) {
					zlo.setYaj(item.getYajin() * 0.8);
				} else {
					zlo.setYaj(item.getYajin());
				}
			}

			or.setOid(zlo.getOid());
			or.setCreatetime(new Date());
			or.setUid(user.getUid());
			or.setOrder_type(3);
			or.setStatus(1);
			if (num == 0) {
				Double price = tai * tim * (zlo.getDanjia() * item.getZheko() + zlo.getXsqj());
				if (genders == 5) {
					int a = liser.findO(user.getUid());
					if (a == 0) {
						price = price + zlo.getYaj() * (tai - 1);
					} else {
						price = price + zlo.getYaj() * tai;
					}
				} else {
					price = price + zlo.getYaj() * tai;
				}
				or.setPayment(price);
			} else {
				Double price = tai * tim * (zlo.getDanjia() * item.getZheko() + zlo.getXsqj()) * 30;

				if (genders == 5) {
					int a = liser.findO(user.getUid());
					if (a == 0) {
						price = price + zlo.getYaj() * (tai - 1);
					} else {
						price = price + zlo.getYaj() * tai;
					}
				} else {
					price = price + zlo.getYaj() * tai;
				}

				or.setPayment(price);
			}
			liser.savezlo(zlo);
			liser.saveor(or);
			session.setAttribute("oid", or.getOid());
		}
		Address address = userService.findAByUid(user.getUid());
		model.addAttribute("address", address);
		model.addAttribute("genders", genders);
		model.addAttribute("item", item);
		model.addAttribute("zlo", zlo);
		model.addAttribute("ord", or);
		return "zupin04";
	}

	// 保存地址
	@ResponseBody
	@RequestMapping("addr")
	public boolean addr(Address addr, HttpSession session) {
		User user = (User) session.getAttribute("user");
		addr.setUid(user.getUid());
		userService.delAddr(user.getUid());
		userService.saddress(addr);
		return true;
	}

	// 跳转付款
	@RequestMapping("fk")
	public String fk(String oid, Model model) {
		Order order = liser.findByO(oid);
		model.addAttribute("oid", oid);
		model.addAttribute("payments", order.getPayment());
		return "zupin06";
	}

	// 验证地址
	@ResponseBody
	@RequestMapping("yanz")
	public boolean yanz(HttpSession session) {
		User user = (User) session.getAttribute("user");
		Address address = userService.findAByUid(user.getUid());
		if (address == null) {
			return false;
		}
		return true;
	}
	
	//查询租赁订单
	/*@RequestMapping("ssss")
	public String finddd(String oid) {
		Order order = liser.findByO(oid);
		
		if(order.getOrder_type()==3) {
			Zulinorder zlo = liser.findByZL(oid);
			Date d=new Date();  
			String time = zlo.getTime();
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			int i = time.length();
			if(time.endsWith("天")) {
				String sub = time.substring(0, i-1);
				Integer s = Integer.parseInt(sub);
				cal.add(Calendar.DATE, s);
				zlo.setGhtime(cal.getTime());
			}else {
				String sub = time.substring(0, i-2);
				Integer s = Integer.parseInt(sub);
				cal.add(Calendar.MONTH, s);
				zlo.setGhtime(cal.getTime());
			}
			zlo.setGhm(1);
			liser.update(zlo);
		}
		return"";
	}*/
}
